<?php

namespace App\Http\Controllers\Admin\Shop;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Shop\Customer;
use GatewayClient\Gateway;

class CustomerController extends Controller
{
    function __construct()
    {
        view()->share([
            '_customer' => 'am-active',
            '_shop' => 'am-in'
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $customers = Customer::customers_all($request);
        return view('admin.shop.customer.index', compact('customers'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    //改变男女性别
    public function change_attr(Request $request)
    {

        Gateway::$registerAddress = '127.0.0.1:1238';

        $id = $request->id;
        $attr = $request->attr;

        $customer = Customer::find($id);
        $customer->$attr = !$customer->$attr;
        $customer->save();

        $data = [
            ['value' => Customer::where('sex', 1)->count(), 'name' => '男'],
            ['value' => Customer::where('sex', 0)->count(), 'name' => '女']
        ];

        Gateway::sendToGroup('sex', json_encode($data));


    }
}
